
from apps.common.base.serializers import *
from apps.pay.order.models import *
import json

class OrderBaseSerializer(BaseSerializer):

    class Meta:
        model = Order
        fields = "__all__"

class OrderModelSerializer(BaseSerializer):

    shopModel = serializers.SerializerMethodField()

    def get_shopModel(self, obj):
        try:
            return {'id': obj.shop.id, 'name': obj.shop.name}
        except:
            return {}

    contentDict = serializers.SerializerMethodField()
    def get_contentDict(self, obj):
        if obj.content:
            return json.loads(obj.content)
        else:
            return []

    itemList = serializers.SerializerMethodField()
    def get_itemList(self, obj):
        try:
            if obj.content:
                content = json.loads(obj.content)
                return content.get('orderItemList') or []
            else:
                return []
        except:
            return []

    userModel = serializers.SerializerMethodField()
    def get_userModel(self, obj):
        if obj.user:
            return {'id': obj.user.id, 'code': obj.user.code, 'phone': obj.user.phone}
        else:
            return {}

    addrinfo = serializers.SerializerMethodField()
    def get_addrinfo(self, obj):
        if obj.addrinfo:
            return json.loads(obj.addrinfo)
        else:
            return {}

    addrinfoDict = serializers.SerializerMethodField()
    def get_addrinfoDict(self, obj):
        if obj.addrinfo:
            return json.loads(obj.addrinfo)
        else:
            return {}
    
    expressageinfoDict = serializers.SerializerMethodField()
    def get_expressageinfoDict(self, obj):
        if obj.expressageinfo:
            return json.loads(obj.expressageinfo)
        else:
            return {}

    paywayLabel = serializers.SerializerMethodField()
    def get_paywayLabel(self, obj):
        if obj.payway:
            return obj.get_payway_display()
        else:
            return '未知'

    sendTypeLabel = serializers.SerializerMethodField()
    def get_sendTypeLabel(self, obj):
        if obj.sendType:
            return obj.get_sendType_display()
        else:
            return '未知'

    orderStatusLabel = serializers.SerializerMethodField()
    def get_orderStatusLabel(self, obj):
        if obj.orderStatus:
            return obj.get_orderStatus_display()
        else:
            return '未知'

    class Meta:
        model = Order
        fields = "__all__"